﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace B13_Ex06
{
    public partial class FormGameProperties : Form
    {
        private const string k_DefaultComputerPlayerName = "Computer";
        private int m_BoardSize;
        private int m_ChangedBoardSize = 0;
        private bool m_HasSecondPlayer = false;
        private bool m_IsChangedSizeBoard = false;
        private bool m_IsChangedPlayerTwoState = false;

        public bool IsChangedSizeBoard
        {
            get { return this.m_IsChangedSizeBoard; }
        }

        public bool IsChangedPlayerTwoState
        {
            get { return this.m_IsChangedPlayerTwoState; }
            set { this.m_IsChangedPlayerTwoState = value; }
        }

        public int ChangedBoardSize
        {
            get { return this.m_ChangedBoardSize; }
        }

        public int BoardSize
        {
            get { return this.m_BoardSize; }
            set { this.m_BoardSize = value; }
        }

        public string PlayerOneName
        {
            get { return this.textBoxPlayer1.Text; }
        }

        public string PlayerTwoName
        {
            get { return this.textBoxPlayer2.Text; }
        }

        public bool HasTwoPlayers
        {
            get { return this.m_HasSecondPlayer; }
            set { this.m_HasSecondPlayer = value;  }
        }

        public FormGameProperties()
        {
            this.InitializeComponent();
            this.textBoxPlayer2.Text = k_DefaultComputerPlayerName;
        }

        private void checkBoxPlayer2_CheckedChanged(object sender, EventArgs e)
        {
            this.buttonOK.Enabled = true;
            this.m_IsChangedPlayerTwoState = true;

            if (this.checkBoxPlayer2.Checked)
            {
                this.textBoxPlayer2.Enabled = true;
                this.m_HasSecondPlayer = true;
            }
            else
            {
                this.m_HasSecondPlayer = false;
                this.textBoxPlayer2.Enabled = false;
                this.textBoxPlayer2.Text = k_DefaultComputerPlayerName;
            }
        }

        private void FormGameSettings_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.DialogResult = DialogResult.Cancel;
            }
        }

        private void buttonDone_Click(object sender, EventArgs e)
        {
            if (this.textBoxPlayer1.Text.Length == 0 || this.textBoxPlayer1.Text.Contains(" ") || this.textBoxPlayer2.Text.Length == 0 || this.textBoxPlayer2.Text.Contains(" "))
            {
                const string warningMessage = "Player names can not be empty or contain spaces.";
                MessageBox.Show(warningMessage, "Damka", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
            else
            {
                if (this.m_BoardSize == 0)
                {
                    this.m_BoardSize = 8;
                    this.m_ChangedBoardSize = this.m_BoardSize;
                }

                this.DialogResult = DialogResult.OK;
                this.Close();
            }

            this.buttonOK.Enabled = false;
            this.ChangeEventsOfSettingsAfterInitialization();
        }

        private void ChangeEventsOfSettingsAfterInitialization()
        {
            this.numericUpDown1.ValueChanged -= new System.EventHandler(this.numericUpDown1_ValueChanged);
            this.numericUpDown1.ValueChanged += new System.EventHandler(this.numericUpDownWhenChangedAgain_ValueChanged);
            this.buttonOK.Click -= new System.EventHandler(this.buttonDone_Click);
            this.buttonOK.Click += new System.EventHandler(this.buttonDoneAgain_Click);
        }

        private void buttonDoneAgain_Click(object sender, EventArgs e)
        {
            if (this.textBoxPlayer1.Text.Length == 0 || this.textBoxPlayer1.Text.Contains(" ") || this.textBoxPlayer2.Text.Length == 0 || this.textBoxPlayer2.Text.Contains(" "))
            {
                const string warningMessage = "Player names can not be empty or contain spaces.";
                MessageBox.Show(warningMessage, "Damka", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
            else
            {
                if (this.m_ChangedBoardSize != this.m_BoardSize)
                {
                    this.m_IsChangedSizeBoard = true;
                }
                else
                {
                    this.m_IsChangedSizeBoard = false;
                    if (this.m_BoardSize == 0)
                    {
                        this.m_BoardSize = 8;
                        this.m_ChangedBoardSize = this.m_BoardSize;
                    }
                }

                this.DialogResult = DialogResult.OK;
                this.Close();
            }

            this.buttonOK.Enabled = false;
        }
        
        private void FormGameSettings_Activated(object sender, EventArgs e)
        {
            this.ActiveControl = this.textBoxPlayer1;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.m_IsChangedSizeBoard = false;
            this.m_ChangedBoardSize = this.m_BoardSize;
            this.buttonOK.Enabled = false;
            this.Close();
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            this.m_BoardSize = int.Parse(((NumericUpDown)sender).Value.ToString());
            this.m_ChangedBoardSize = this.m_BoardSize;
        }

        private void numericUpDownWhenChangedAgain_ValueChanged(object sender, EventArgs e)
        {
            this.m_ChangedBoardSize = int.Parse(((NumericUpDown)sender).Value.ToString());
            this.buttonOK.Enabled = true;
        }

        private void textBoxPlayer1_TextChanged(object sender, EventArgs e)
        {
            this.buttonOK.Enabled = true;
        }

        private void textBoxPlayer2_TextChanged(object sender, EventArgs e)
        {
            this.buttonOK.Enabled = true;
        }
    }
}
